package org.penguin.study.bk.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.penguin.study.bk.base.util.IdUtil;
import org.penguin.study.bk.mq.model.RabbitQueue1Message;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author chengyinghua
 */
@Configuration
@ConditionalOnProperty(name = "rabbitmq.enable", havingValue = "true")
public class RabbitMqConfig {

    private static final boolean NON_DURABLE = false;
    public static final String topicExchangeName = "spring-boot-exchange";
    public static final String queue1Name = "spring-boot.queue1";
    public static final String queue2Name = "spring-boot.queue2";
    public static final String queue3Name = "spring-boot.queue2";

    public static final String queue1RoutingKey = "foo.bar.baz";
    public static final String queue2RoutingKey = "*.important.*";
    public static final String queue3RoutingKey = "#important.*";


    @Autowired
    private CachingConnectionFactory rabbitConnectionFactory;

    @Autowired
    private ObjectMapper objectMapper;

    @Bean
    RabbitTemplate rabbitTemplate() {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(rabbitConnectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter(objectMapper));
        return rabbitTemplate;
    }

    @Bean
    public MessageConverter jsonMessageConverter(ObjectMapper objectMapper) {
        return new Jackson2JsonMessageConverter(objectMapper);
    }

    @Bean
    public Declarables fanoutBindings() {
        Queue queue1 = new Queue(queue1Name, NON_DURABLE);
        Queue queue2 = new Queue(queue2Name, NON_DURABLE);
        Queue queue3 = new Queue(queue3Name, NON_DURABLE);

        TopicExchange topicExchange = new TopicExchange(topicExchangeName);
        return new Declarables(
                queue1,
                queue2,
                queue3,
                topicExchange,
                BindingBuilder.bind(queue1).to(topicExchange).with(queue1RoutingKey),
                BindingBuilder.bind(queue2).to(topicExchange).with(queue2RoutingKey),
                BindingBuilder.bind(queue3).to(topicExchange).with(queue3RoutingKey));
    }

    @Scheduled(fixedDelay = 5000)
    public void pushMessage() {
        String messageString = "Rabbit消息测试@" + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME);
        RabbitQueue1Message message = new RabbitQueue1Message(IdUtil.generateUUID(), "Penguin组织", messageString, LocalDateTime.now());
        rabbitTemplate().convertAndSend(RabbitMqConfig.topicExchangeName, queue1RoutingKey, message);
    }
}